/**
 * UploadDAO.java
 * File upload DAO super class.
 */
package com.example.myproject;

import java.util.List;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.util.DAOBase;

public class UploadDAO extends DAOBase{

    static {
        ObjectifyService.register(UploadDTO.class);
    }

    /**
     * Saves a DTO to the blobstore.
     * @param dto The DTO to be saved.
     */
    public UploadDTO save(UploadDTO dto) {
        ofy().put(dto);
        return dto;
    }
    
    /**
     * Retrieves DTOs from the blobstore with matching IDs.
     * @param id Supplied search parameter.
     * @return Returns a key map of DTOs matching id.
     */	
    public UploadDTO findById(Long id) {
    	if(id <= 1L) return null;
        Key<UploadDTO> key = new Key<UploadDTO>(UploadDTO.class, id);
        return ofy().get(key);
    }
    
    /**
     * Retrieves a list of all DTOs in the blobstore.
     * @return List containing all DTOs in the blobstore.
     */    
	public List<UploadDTO> list() 
	{		
		List<UploadDTO> alist = ofy().query(UploadDTO.class).list();
		return alist;
	}
	
    /**
     * Deletes a DTO from the blobstore, specified by id.
     * @param id Supplied id of the DTO to be deleted.
     */
	public void delete(Long id)
	{
		if(id <= 1L) return;
		UploadDTO upload = ofy().query(UploadDTO.class).filter("id ", id).get();
		ofy().delete(upload);
	}
	
}
